<?php
namespace app\index\controller;
use think\Controller;
use geetest\lib\GeetestLib;
use think\Session;
use think\Cookie;

define("CAPTCHA_ID", "48a6ebac4ebc6642d68c217fca33eb4d");
define("PRIVATE_KEY", "4f1c085290bec5afdc54df73535fc361");


class Index  extends Controller
{
    public function index()
    {
        return view('index/index', [
            'loginname' => Cookie::get('loginname')
        ]);
    }

    // 加载登录页
    public function login()
    {
        return view('index/login');
    }

    // 执行登录
    public function doLogin()
    {
        $data = input('post.');
        // var_dump($data); die;
        $customer_name = $data['customer_name'];
        $customer_password = $data['customer_password'];

        // var_dump($data,$customer_password);
        $result = db('customer')->where(['customer_name' => $customer_name])->find();
        Session::set('index_user', $result);
        $logindata['customer_name']= $result['customer_name'];
        $logindata['id']= $result['id'];

        // var_dump($result); exit;
        // 验证该用户是否有效
        if ($result['status'] !== 1) {
            $this->error('该用户无效!');
            exit;
        }

        if (!$result) {
            $this->error('用户名不存在！');
            exit;
        }
        // 验证密码
        if ($result['customer_password'] != md5($customer_password)) {
            $this->error('密码不正确');
            exit;
        }

        if($result)
        {
            if (!empty($data['checkbox']))
            {   // 如果勾选了记住密码就设置7天的cookie;
                Cookie::set('loginname',$result['customer_name'],'604800');
                Cookie::set('loginid',$result['id'],'604800');
            }else{
                // Session::set('index_user',$logindata);
                Cookie::set('loginname',$result['customer_name']);
            }
                $this->redirect('index/Index/index');
        }else{
                $this->error('你的账户或者密码输入有误');
        }

        //把用户信息添加到session
        // Session::set('index_user', $result);
        // return $this->redirect('index/Index/index');
    }

    // 加载注册页
    public function register()
    {
        return view('index/register');
    }

    // 执行注册
    public function doRegister()
    {
        $data = input('post.');
        var_dump($data);
        $customer_name = $data['customer_name'];
        $customer_password = $data['customer_password'];
        $result = db('customer')->where(['customer_name' => $customer_name])->find();
        // var_dump($result['status']); exit;
        // 验证该用户是否有效
        if ($result['status'] === 1) {
            
            return $this->redirect('index/Index/register');
        }

        $data = [
            'customer_name' => $customer_name,
            'customer_password' => MD5($customer_password)
        ];
        // var_dump($data); exit;

        $customer = db('customer')->insert($data);
        // var_dump($customer);exit;
        if ($customer) {
            $this->redirect('index/Index/login');
        } else {
            $this->error('注册失败');
        }
    }

    // 退出登入
    public function loginOut()
    {
        Session::delete('index_user');
        Cookie::delete('loginname');
        Cookie::delete('loginid');

        $this->redirect('index/Index/index');

    }

    // 登录选字验证
    public function checkGeetest()
    {
        $GtSdk = new GeetestLib(CAPTCHA_ID, PRIVATE_KEY);
        session_start();

        $data = array(
                "user_id" => "test",
                "client_type" => "web", // web: pc端浏览器; h5: 移动端浏览器
                "ip_address" => "127.0.0.1" // 请在此处传输用户请求验证时所携带的ip
        );

        $status = $GtSdk->pre_process($data, 1);
        $_SESSION['gtserver'] = $status;
        $_SESSION['user_id'] = $data['user_id'];
        echo $GtSdk->get_response_str();

    }

    public function _empty(Request $request)
    {
        return view('/error');
    }

}
